Post comments using post ids Wordpress - wordpress

I’m trying to use blogs(posts) with two different designs
First, for non-loggedin(guest) users which access blogs from website (Example URL – Click Here)
Second is for logged in users which access blogs from their frontend dashboard
For the first case, I am using single.php and
For the second case, I’m using custom template in user’s dashboard and fetching posts using post ids.
QUERY
——————–
So the question is how to post comments to post using post id i.e for the second case?
E.g post url is: http://yourdomain.com/blogs/?pid=23
Here is attached video which highlights the query
Unable to display content. Adobe Flash is required.
Further, is there any other way to achieve the above scenario i.e. two different designs
Let me know in case of more clarification.

You can use a normal loop in your custom query for logged in users
for example
<?php
$args = array(
'post__in' => array(1,2,4,6),
'post_type' => 'post',
);
// the query
$the_query = new WP_Query( $args ); ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
// put your code here
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
this way you should not be worry about getting comments by post id (the loop will handle the id for you)

Related

Add sticky post to a custom post type wordpress

My version of wordpress is 5.2.3
I have a custom post type and I want to add it a sticky post.
I tried many plugins and hack to figured out how to solved this problem (sticky custom post type isn't a native feature of wordpress) but I still don't know how to solved it.
Does anyone have a solution for me ?
-----EDIT------
I try this plugin https://wordpress.org/plugins/custom-post-type-sticky/. He put the same feature than the classic post of wordpress (a checkbox for put a sticky post) he works perfectly in back but nothing appear in front. (event with an additional code found here https://www.sktthemes.org/wordpress/add-sticky-posts-wordpress/) My code is a basic loop like :
<?php
$args_ressource = array(
'post_type' => 'ressource',
'order' => 'DESC',
'posts_per_page' => 4,
);
$ressource = new WP_Query( $args_ressource );
if ( $ressource->have_posts() ) { ?>
<ul>
<?php
while ( $ressource->have_posts() ) {
$ressource->the_post();
$imageArticle = get_field('hero_image');
?>
<li>
<a href="<?php the_permalink(''); ?>">
<?php the_title(); ?>
</a>
</li>
<?php } ?>
</ul>
<?php }
wp_reset_postdata();
?>
To be precise I need to show my sticky custom post type on the left of my page, and on the right the rest of the custom post but without this sticky post.
With the basic article a simple loop works because this is a basic feature of wordpress, but not with custom post type
Many thanks
admin:
Create meta_value mimicking sticky function on/off
logic:
query "posts" filtering only checked posts
create second query where you exclude above "post" ids
template:
run a loop for sticky
run the common loop

Wordpress category

This is code from my wordpress homepage template. There are 3 same code, like this one bellow. The question is how can I put different categories to show different news when I post. At the moment code is same on my homepage and it show only category named World, it's the first category created. How can I setup the rest of code with different categories.
Currently it's like this : http://zaslike.com/files/os8hvxg38oendfwcvrz6.png
I need to put other categories instead same three.
http://justpaste.it/e0nh - Here is the code of homepage template.
I believe you are asking for this:
<!-- Your Query -->
<?php // The Query
query_posts( array ( 'category_name' => 'change-to-category-name', 'posts_per_page' => 1 ) ); ?>
<?php // The Loop
while ( have_posts() ) : the_post();?>
// YOUR HTML GOES HERE
<?php endwhile; ?>
<?php // Reset Query
wp_reset_query(); ?> // DON'T FORGET THIS
Use this code at the parts of the site that display your categories change the categories name to the ones you want to display and you are good to go.
Good luck welcome to SO.

how do I make a list of my postings on Wordpress 3.4.2. and theme Twenty Ten

I've tried to find a solution to allow me to make an "Index" of postings I have published (349 posts, 27 pages, 28 categories and 50 tags).
Is there a simple solution allowing me to display, as a page or post, an "index" of ALL posts, pages, etc. with WordPress?
An "Index Page" where readers and/or subscribers can click on a post/page and be taken to that particular posting.
I've tried various plugins - (Index Press), (Table of Contents Plus) to mention just a couple but, unless I'm doing something wrong, nothing seems to work for me.
<?php wp_get_archives('type=alpha'); ?>
This will list all posts in alphabetical order.
More info can be found in the Codex here: wp_get_archives
WP Query is your best friend.
This will give you a list of all posts and pages in the form of links. If you have any additional post types, add the slug to the post_type array.
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => array('post', 'page')
);
$q = new WP_Query($args);
if($q->have_posts()) : while($q->have_posts()) : $q->the_post();
?>
<?php the_title(); ?><br/>
<?php
endwhile;endif;
?>

How do you display a random post from your database of content in a wordpress 3.0 theme?

I am developing a webpage at http://www.knowledgenation.us and currently I have roughly 500 posts on the page. I believe that is far too many posts to expect someone to read through but I do believe that my page has return value. I want people to return to the webpage on a regular basis and always get something new from the site.
That being said, I would like to post three random posts from my database to the body of the web page that is the theme that I have. I would also like to know how to make that code modular so that I can reuse it for a new incarnation of this website that is going to pull in content from RSS feeds from two websites that friends of mine are developing.
That all being said, bottom line, how do you post random posts to a website, what would the code look like and please be kind in explaining because I am quite new to PHP programming and would not understand most of what the code is about. I just recently got a http://www.lynda.com account and am going to be learning all about PHP but for right now I understand little.
I thank you in advance for helping me with this.
When you query your posts, you can pass on query attributes such as a category, included/excluded post ids, limits and offsets, etc.. You can also define how your results will be ordered — by which field and into which direction (ASC/DESC).
The order_by parameter can be a regular field names like title or date, and also rand as in random to fetch random posts.
Here's an example to use outside the loop, fetching five random posts:
<ul>
<?php
$args = array('numberposts' => 5, 'orderby' => 'rand');
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
And another example for a regular loop:
<?php
$args = array('numberposts' => 5, 'orderby' => 'rand');
query_posts($args);
while (have_posts()) : the_post();
the_content('Read the full post »');
// And so forth…
endwhile;
?>
Hope you get the picture…

WordPress custom query pagination

I have a WordPress site, where on the main page I list the content from more categories.
My question is, is there a plugin where I can paginate the results from a category? I mean something like $this->plugin_paginate('category_id'); or smth?
Best Regards,
If you're using the standard Wordpress loop, even with a query_posts for a category, pagination is automatic with the usual posts_nav_link. Are you trying to paginate for more than one query and more than one category on the same page?
Edit 11/20: I use this in several different places on one page to show the latest post in a category:
<?php
$my_query = new WP_Query('category_name=mycategory&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
That link then goes to a category page that paginates for that category: Category Templates « WordPress Codex
I don't know how to paginate different categories on the same page. Must be possible. Maybe ask in the Wordpress forums.
This sounds like something that a simple, well-formed query_posts() call can do. I doubt that you'll even need to rely on a plugin. :)
I'm going to assume that you're familiar with the query_posts() function, so let's go ahead and use this example as a base:
// let's get the first 10 posts from category ID 3
query_posts('posts_per_page=10&cat=3');
while(have_posts()):the_post();
// do Wordpress magic right here
endwhile;
Now, to get the 11th to 20th posts from category 3 (that is, the NEXT 10 posts), we'll want to use the [offset] parameter of query_posts():
// let's get the next 10 posts from category ID 3
query_posts('posts_per_page=10&cat=3&offset=10');
while(have_posts()):the_post();
// do Wordpress magic right here
endwhile;
For most purposes, that should be sufficient. However, you did mention that you plan on paginating category post lists from the main page alone? I'm assuming that you mean that you have multiple category post lists on your main page, and all these are paginated independently.
With something like that, it looks like you'll have to work a bit with Javascript to do the work for you, along with what I illustrated above.
I believe you could do something like this:
<?php
if(isset($_GET['paged'])){
$page = $_GET['paged']-1;
}else{
$page = 0;
}
$postsPerPage = 5;
$theOffset = $page*$postsPerPage;
?>
<?php query_posts(array('posts_per_page' => $postsPerPage, 'cat' => CATEGORIES HERE, 'offset' => $theOffset)); ?>
Hope this will help you :)
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'paged' => $page,
);
query_posts($args);?>
?>

Resources