Custom wordpress post content by examples

WordPress usually display post content by the_content() or get_the_content() function, these content is edited by wordpress editor, so wordpress display what you have written.
Consider one situation, you want to display similar posts in single page, and you add post id, for example:1,2,3 by wordpress editor, if you do nothing, wordpress will display:1, 2, 3.
If you want to display posts that id is 1,2,3, you can do by add_filter() function.

Examples:

1.Custom post content by add_filter

function get_similar_posts($content){

if ( is_single() ){

$post_ids = .....// get post id from $content

// query and save content in $content

return $content;

}

}

add_filter("the_content",get_similar_posts);

Learn difference wordpress the_content() and get_the_content() by examples

WordPress the_content() function is almost the same as get_the_content(), it’s defined as:

function the_content( $more_link_text = null, $strip_teaser = false) {
 $content = get_the_content( $more_link_text, $strip_teaser );
 
 /**
 * Filters the post content.
 *
 * @since 0.71
 *
 * @param string $content Content of the current post.
 */
 $content = apply_filters( 'the_content', $content );
 $content = str_replace( ']]>', ']]>', $content );
 echo $content;
}

From code above, the_content() function can not return the post content, however, it add a apply_filters and process “]]>”

Learn wordpress the_content() by examples

Description:

the_content( string $more_link_text = null, bool $strip_teaser = false )

Display the post content, but do not return the content. This function often be used in the loop

Examples:

1.Display the post content

<?php while ( have_posts() ) : the_post() ?>
<div id="post-<?php the_ID() ?>" class="<?php barthelme_post_class() ?>">
<div class="post-container">
<div class="post-content">
<h2 class="entry-title"><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
</div>
</div>
</div><!-- .post -->
<?php endwhile ?>

Learn python assert statement by examples

Description:

Assert statements are a convenient way to insert debugging assertions into a program.
You can use it by :
assert <condition>

assert <condition>,<error message>
When condition is False, python application will raise an exception, error message will be displayed, True and application will continue.

Examples:

1.Make sure inputs and targets array dimensions are the same.

def iterate_minibatches(inputs, targets, batchsize, shuffle=False):

    assert len(inputs) == len(targets),"inputs and target is not the same"

    ......

Add wordpress post to categories using php code by examples

To add a post to wordpress categories by php programming, you can do like this:

Step 1. Load the WordPress environment

<?php
$inc = "E:\\xampp\\htdocs\\www.programmingbyexamples.com\\wp-load.php";
require_once($wordpress_inc_path);
?>

Step 2. Add a post to wordpress categories

Before adding, you must be sure that these categories exist and know their category id, if these categories do not exist, you will can not bind them.

<?php
$post_id=1;
$post_cats=array(8,9);
$result = wp_set_post_terms( $post_id, $terms = $post_cats, $taxonomy = 'category', $append = false );
print_r($result);
?>

Code above add post,which id = 1, to categories, their id = 8 and 9

Apply .sty file in LaTex by examples

If you have download a .sty file which named emnlp2018.sty, how to use it?

Step 1: Create a new latex document

You can create a new latex document in path:d:\latex-document

Step 2: Copy emnlp2018.sty to d:\latex-document

You can copy this .sty file to the same folder of the document you created

Step 3: Use \usepackage command in your document

like this:

\documentclass{article}
\usepackage{emnlp2018}
.....

Step 4: Complie your document

If you met problem:! LaTeX Error: File `emnlp2018.sty’ not found. Please check step 2.

Fix python theano.tensor.argmax() return argmax by examples

Consider python source code below:

import theano
import theano.tensor as T
import numpy

share_x = theano.shared(value = numpy.array([[1,2,3],[2,2,4]]), name = "x")

y = T.argmax(share_x)

print y

You can get output:argmax, not the index of the max number, why?

To theano function theano.tensor.argmax(), the input variable must be a Theano TensorVariable type, this function describes a computational graph, so you can not get index.

To use this function correctly, you can use theano.function() and read:

Learn python theano.tensor.argmax() by examples

Learn python theano.tensor.argmax() by examples

Description:

theano.tensor.argmax(x, axis=None, keepdims=False)

return the index of the maximum value along a given axis

Parameters:

x: a Theano TensorVariable type
axis: axis along which to compute the index of the maximum

Examples:

1.Get the max number Index in a maxtix

import theano
import theano.tenser as T
import numpy

Define a matrix

matrix_x = T.dmatrix()

max_index = T.argmax(matrix_x)

get_max_index = theano.function([matrix_x], max_index)

matrix_input = numpy.asarray([[1, 2, 3],[2, 2, 4]])

print get_max_index(matrix_input)

output:5

If you want to get max number index by row, you shoud edit T.argmax

max_index = T.argmax(matrix_x, axis= 1)
get_max_index = theano.function([matrix_x], max_index)

matrix_input = numpy.asarray([[1, 2, 3],[2, 2, 4]])

print get_max_index(matrix_input)

output:[2, 2]