I have a Wordpress site and I am trying to fetch posts from a specific category. That works, but now I want to link to the specific posts with a permalink. I could not getting it to work with escaping in a foreach. Can someone tell me/show me what I am doing wrong here?
<?php
global $post; // required
$args = array('category' => 9); // include category 9
$custom_posts = get_posts($args);
foreach($custom_posts as $post) :
setup_postdata($post);
echo "<a href='.the_permalink().'> the_title() </a> ";
the_excerpt();
//and so on..
endforeach;
?>
You're not actually closing your quotes properly and aren't using the correct functions. Line 5 should read more like:
the_title("<a href='".get_permalink()."'>",'</a>',true);
BOOM.
Related
I am using HTML5Blank as a starting theme and it comes with this function which returns 40 chars excerpt:
<?php html5wp_excerpt('html5wp_custom_post'); ?>
My main blog page is more complex, so I am using array to store values into it and echo them where I need them:
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $post_titles[$counter] = get_the_title($post->ID); ?>
<?php $post_excerpts[$counter] = html5wp_excerpt('html5wp_custom_post', $post_id); ?>
<?php $post_permalinks[$counter] = get_the_permalink($post->ID); ?>
<?php $post_thumbs[$counter] = get_the_post_thumbnail($post->ID, '', array('class' => 'img-fluid')); ?>
<?php $counter++; ?>
<?php endwhile; ?>
All other fields work and I can echo them, but I don't have any idea how to make excerpt work because it isn't echoing anywthing with:
<?php echo $post_excerpts[0]; ?>
First of all I notice, that you are using a variable called $post_id, which is not defined as far as I can see. You need to add a $post_id = $post->ID; before or alternatively you can use instead:
<?php $post_excerpts[$counter] = html5wp_excerpt('html5wp_custom_post', $post->ID); ?>
Maybe this already solves your problem. But to make sure, I will take it further.
I took a look at the functions.php of the HTML5-Blank-Theme: https://github.com/html5blank/html5blank/blob/master/src/functions.php
// Create 40 Word Callback for Custom Post Excerpts, call using html5wp_excerpt('html5wp_custom_post');
function html5wp_custom_post($length)
{
return 40;
}
So the function only returns the value of 40. I guess you can simply use the html5wp_excerpt() like this:
html5wp_excerpt(40);
Maybe there is something wrong with the html5wp_custom_post, so you can get rid of it to test this. And I also think, why use an addtional function, if it only returns a number... you can easily set it in the function call.
I don't know if this functions accepts an post ID as a parameter. So maybe it can only be used inside of a single.php page. I can't find a documentation about this, maybe you can do some research and find it out.
Here is another way to achieve it:
You can use the get_the_title() function that will accept the post id. Unfortunately, the get_the_excerpt() does not accept it.
So we first need to get the post object and then apply a filter to get the excerpt of the post. Do this by putting this code inside your while loop:
<?php $current_post = get_post($post->ID); ?>
You now have the current post as an object. In the next line, we apply the filter and save the result to the array at the right index position:
<?php $post_excerpts[$counter] = apply_filters('get_the_excerpt', $current_post->post_excerpt); ?>
I wonder why there are so many php opening and closing tags, so you could make your code more readable:
<?php while ($the_query->have_posts()) : $the_query->the_post();
$current_post = get_post($post->ID);
$post_excerpts[$counter] = apply_filters('get_the_excerpt', $current_post->post_excerpt);
$post_titles[$counter] = get_the_title($post->ID);
$post_permalinks[$counter] = get_the_permalink($post->ID);
$post_thumbs[$counter] = get_the_post_thumbnail($post->ID, '', array('class' => 'img-fluid'));
$counter++;
endwhile; ?>
Just as an additional info, you could also use only the filters, should work with your post object:
$post_titles[$counter] = apply_filters('the_title',$current_post->post_title);
EDIT:
You can trim your excerpt to a certain length with mb_strimwidth (read more: https://www.php.net/manual/en/function.mb-strimwidth.php) :
$current_post = get_post($post->ID);
$trim_excerpt = apply_filters('get_the_excerpt', $current_post->post_excerpt);
$post_excerpts[$counter] = mb_strimwidth($trim_excerpt, 0, 40, '...');
EDIT 2:
Maybe you should check if you are getting your post object. The fact that you are always seeing the same excerpt, maybe means you get the excerpt of the current page (not the post in your query).
$current_id = get_the_id();
$current_post = get_post($current_id);
Someone explain to me what either the server or browser is doing here:
Integrated a wordpress blogs last 3 posts to display on home page of non wordpress site and simply was going to apply some inline CSS styling to the post:
<?php
$posts = get_posts('numberposts=3&order=ASC&orderby=post_title');
foreach ($posts as $post) : setup_postdata( $post );
echo '<span style="color:blue;font-size:10px;">'.the_date().'</span><br>';
the_title();
the_excerpt();
endforeach;
?>
The styling does not occur because the echo line is being rendered by the browser/server/god/chaos or whatever AFTER the the_date() variable????
Actual rendered code:
<div id="blog-box">
November 11, 2013<span style="color:blue;font-size:10px;"></span><br>Hello world!<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!</p>
</div>
Sorry if I'm missing something obvious but i don't get why this is not surrounding the date with the span code being echoed.
Thanks.
the_date() has a built in echo.
Try this
echo '<span style="color:blue;font-size:10px;">';
the_date();
echo '</span><br>';
I am working on a portfolio website with multiple artists, and each artist will have their own blog.
For each artist to have their own blog I decided for them to 'tag' themselves in posts that they make, on their portfolio pages I will list the posts with said tags.
The name of the page, is their name, so initially I thought just using:
<?php
$args=array('posts_per_page'=>2, 'tag' => $post->post_title);
$wp_query = new WP_Query( $args );
?>
would work, but doesn't seem to pull up any results. Yet when I echo the post title and the tag they are both displayed exactly the same.
So my next thought was to match the tag to a reg expression. Something like:
<?php
if( preg_match("/tony/i",$post->post_title)){
echo "Tony";
}
?>
but I do not know how to work that into a wp query.
Any idea how to do this, or if there is a better way to get to the same end result?
I quite recently was baffled by the exact same issue. I was trying to display articles based on a combination of a first and last name, but that wasn't working by just using the tag argument. I actually ended up using tag_slug__and for mine. Here's what I came up with...
<?php
$original_query = $wp_query;
$args = array( 'tag_slug__and' => array(strtolower($first_name) . "-" . strtolower($last_name)) );
$wp_query = new WP_Query( $args );
if(have_posts()):
echo "<h3>News Tagged " . $first_name . " " . $last_name . "</h3>";
while(have_posts()) : the_post();
$title = get_the_title();
$content = get_the_content();
$date = get_the_date();
//use the vars for something suuhhhhweeeeet!
endwhile;
endif;
$wp_query = $original_query;
wp_reset_postdata();
?>
The reason I used the tag_slug__and was because the tag slug is the lowercase tag with spaces delimited by a -. This is probably not the ideal solution, and just feels hacky, but it does work.
I'm curious why you would use tags, and not make use of the author page template and the native WordPress author system? There are lots of ways to do this without tags. If you need multiple authors, check out the co-authors plus plugin.
http://wp.tutsplus.com/tutorials/how-to-create-a-wordpress-authors-page-template/
http://wordpress.org/extend/plugins/co-authors-plus/
I'm trying exhaustively to get a specific row from within a loop using the_meta() array in Wordpress and assigning a variable to it (like the procedural while loop). Right now it simply pulls all of the rows at once. The result from this query returns 3 rows from the database. What I am trying to do it wrap a div tag around one of the returned rows that is stored within the_meta();.
I've tried exploding the array and returning the first row but it just returns everything all at once. I just want to get a row and put it in a variable so that I can style it using CSS.
Here is the code:
$args = array(
'post_type' => 'membersprofile',
'posts_per_page' => 20);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$newvar = explode(" ", the_meta());
echo $newvar[0];
endwhile;
Any help would be greatly appreciated!
EDIT: Thanks to Youn for pointing me in the right direction and finding the answer for me, The problem was I was using the_meta() which only returned the whole rows. By using get_post_meta I was able to assign variables to each row returned. The code that works is:
$key_2_value = get_post_meta(get_the_ID(), 'wpcf-shortbio', true);
// check if the custom field has a value
if($key_2_value != '') {
echo $key_2_value;
}
Hope this helps someone else!
Try using get_post_meta() in place of the meta. It will return the meta value for a specific page/post.
For more info visit at http://codex.wordpress.org/Function_Reference/get_post_meta
In loop you can use
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<p><?php echo post_custom( 'my_meta_key' ) ; ?></p>
<?php endwhile; endif; ?>
Can someone tell me what the PHP would look like in order to get a category from Wordpress and then print it where ever I want?
I'm guessing I have to build a PHP function. Something like:
function get_a_category() {
$category = get_the_category(); <-----( not sure how to ge ta specific category )
echo $category;
}
I have no idea I know nothing about PHP
can someone help me please ?
You don't have to write your own function; you do have to work with the The Loop (WordPress Codex) and a new query (Function Reference/query posts « WordPress Codex) to keep the original WP loop in place. Run this new query in a page template to get the first post from "mycategory". Change showposts to the number of posts you want.
<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php endwhile; ?>
Try this
function get_a_category() {
$category = get_the_category();
foreach ($category AS $key => $value) {
$category_name[] = $value->cat_name;
}
$categories = implode(', ',$category_name);
echo $categories;
}