Automatically insert content into News section - php

I'm developing a Wordpress theme. One of the sections on the front page is titled News. In that section the user should be able to insert information himself (I'll make a separate file called news.php which will php included into the markup), but I also want the content to be automatically updated once the user makes a post.
For example, if the user writes a new post, I want the news section to be automatically updated to something like this:
A new post was written, find it here (where 'here' is a hyperlink pointing to the url of the post).
edit Let me try to give a little more details so it's clearer.
The news section will be an unordered list.
Thus:
<ul>
<li>
News item 1
</li>
<li>
News item 2
</li>
</ul>
I want the user to be able to add content to the news section, aka make new News Items, but using the visual editor of Wordpress such that the user doesn't have to understand the code and doesn't have to copy/paste the LIs.
Furthermore, whenever a new post is published, I want it to show up as:
"new post published, find it here" where 'here' is a hyperlink with linking to the post.
Is this possible to accomplish?
Thanks, Amit

make a Categorie, or a Custom Post Type called, "News" so when the user creates a new post, but wants it to be in the "News" section all he has to do is click on the Category "News." You could also create a custom post type, just for "News" and have custom taxonomies etc as well.
Then when you want to print the News post in your theme, write use the query post function and limit the loop to "category_name=news"

If i correctly understood, you need write something like this:
<?php
query_posts(array('posts_per_page' => 1));
the_post();
?>
A new post was written, find it here
Latest news list
<?php
query_posts(array('posts_per_page' => 6));
?>
<ul>
<?php $count=0; if (have_posts()) : while (have_posts()) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; endif; ?>
</ul>

Related

How to gather attribute of a wordpress page on a outside page?

I'm new at Wordpress. I'm making a site, and i'm making it directly on code. Perhaps, i have a doubt: Let's say i have on my blog (site.com/blog) a attribute that is, for example, street-location.
I'm on my home page, and i want to do a "blog" section, where i will only show the most relevant news of the blog external page. For example:
<h1> this is the news from the street <?php bloginfo = 'street-location'?> </h1>
Example: I have a real estate theme. There are many things composing the building, including, on the same example, street-location. On the buildings page, i'll set some street adress to "street-location", and that will be INSIDE Wordpress.
What i need to know is: OUTSIDE Wordpress (if i create an external folder, for example), how can i get this attribute?
Do it work like i described above? How do i link this?
Wait for the answer, thanks!
You can use a WP_Query to find the relevant posts.
$query = new WP_Query(['category_name' => 'street-location']);
<?php if ($query->have_posts()): ?>
<?php while ($query->have_posts()): $query->the_post() ?>
<h2><?= get_the_title($query->post->ID) ?></h2>
<?php endwhile ?>
<?php wp_reset_postdata() ?>
<?php endif ?>
Read the documentation for details of the other available query arguments. https://codex.wordpress.org/Class_Reference/WP_Query

Adding content to wordpress page

So, i finally got my css and js scripts to load on WP
Now there is but one thing i need to get done.
i have my own theme, including header.php, footer.php, page.php
header.php and footer.php is working just fine, loading scripts and showing properly, but now i need to add all the other content.
my page.php is currently:
<?php /* Template Name: CustomPageT1 */ ?>
<?php get_header(); ?>
<?php the_content(); ?>
<?php get_footer(); ?>
I would need to somehow add html content to pages, i have about 20 ready made .php pages which needs to be transfered to WP.
Soooo how do i go about making new page (pages -> add new) and using page template while getting the html content to show up?
I've tried to just make new page and in text mode add up all the html into page, but it only shows empty page with header and footer, so the problem is most likely the page.php and i have no idea how to get it to work.
You can do look like this:
<?php /* Template Name: CustomPageT1 */ ?>
<?php get_header(); ?>
<?php
while ( have_posts() ) : the_post();
the_content();
endwhile;
?>
<?php get_footer(); ?>
You are on the good way. While developing a custom theme from scratch is a great challenge it's not too hard.
I could recommend to take it easy and follow this tutorial I found really helpful some time ago, I learned a lot there:
Developing a WordPress Theme from Scratch
You must have the official source documentation always in your mind:
Theme Development
Do some reading and you will see that making themes is really fun and gratifying :)
EDIT:
I would recommend picking a good starter theme or framework and work using child themes. You have plenty of them to pick.
To get started adding a new page to your WordPress site, find the Pages menu in the WordPress Dashboard Navigation menu. Click Add new.
The WordPress page editor looks nearly identical to the post editor, except for a few different boxes located on the right side of the screen.
Add the title of the page, like About. Note: If you have pretty permalinks set up, the title of your page will also be the URL slug.
Next, add some content.
The Publish section of the page editor is exactly the same as for writing posts. When you’re ready to publish, you can either publish immediately, save this or a draft, or schedule the page to be published later.
The Page Attributes section applies a parent page and template to your new page. For the Parent section, you can arrange your pages into hierarchies. For example, you could create this new page with additional pages under it. There are no limits to how many levels you can nest pages.
Some WordPress themes have custom page templates, so the next Template section allows you to apply a template to your new page.
The Order box allows you to order your page numerically. Pages are usually ordered alphabetically, but you can choose your own order by entering a number in this field.
Preview the page one last time, then click Publish. You’ve added a new page to your WordPress site.
This is how your
your index.php should look like :
<?php
get_header();?>
<div class="YourContainer">
<div class="Whatever">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="ContentSectionDiv">
<?php the_content();?>
</div>
<?php endwhile; ?>
<?php else: ?>
<?php endif; ?>
</div>
</div>
<?php get_footer();?>
you can make also a custom loop
<?php
$arg = array("post_type" => "services",
"posts_per_page" => 9,
"order_by" => "date",
"order" => "ASC",
);
$services = new WP_Query($arg);
if ($services->have_posts()):;
while ($services->have_posts()):$services->the_post();
the_content();
endwhile;
endif;
?>

Conditional Wordpress Header

I'm trying to create a header to be used in my WP posts so that it displays a list item and and anchor according to the post's author. For example, if, and only if, the author has ID of 6, the header will have one more list item with an anchor to, let's say, page A. If it's not that author I don't want it to display the list item.
So in my single-post.php template I load this header I want with:
<?php get_header('bananas'); ?>
and in header-bananas.php I have:
<nav>
<ul>
<li>Home</li>
<li>Clinic's Blog</li>
<?php if (!is_author(6)){ ?>
<li id="apples">Link to page A</li>
<?php } ?>
</ul>
</nav>
However I suspect Wordpress is not looping in the header so it can't detect if the conditional is true before loading the post in single-post.php, because #apples is displaying even if that post isn't written by author of id 6.
Am I right? If so, what's the best approach to load a conditional with a Wordpress function in the header? Thank you very much!
I was using the wrong function.
The correct way of doing this would be something like this:
<?php
$myauthor = get_the_author_ID($post->ID);
if ($myauthor == 6){
echo '<li id="apples">Link to Page A</li>';
};
?>

two separate blogs on one wordpress site

I did find one topic that sort of touched base on this but if there is a better link please advise.
I have a self hosted Wordpress website set up typically for a static home page and a separate page that shows recent posts called "Reviews". I need to write posts under a category "Recent News" that would be excluded from the recent post and shown on their own page, a second blog page.
The Codex tells me to add this to the index.php to exclude the category "recent-news":
<?php
if (is_home()) {
query_posts("cat=-3");
}
?>
with 3 being "recent-news". This does nothing no matter where I place it on the page. It still shows up with the recent posts. I'm guessing I'm in the wrong section for excluding categories in the Codex.
How do I exclude a category from recent posts, add a new page called "Recent News" and have it only show posts from the 'recent-news" category.
or have I climbed down a rabbit hole...
you can do it like this: edit your index.php page or create a custom template and then create a page (say "Front Page" ) and assign this template to it. In this template, copy your index.php code or if you feel confident simply create your template and include this code:
<?php if (have_posts()) : ?>
<?php $my_query=new WP_Query( 'cat=-3'); while
($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?>
<div class="post">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<?php endwhile;?>
<?php endif;?>
About your second question, simply go to Appearance --> Menu and there you'll see you can add pages but also categories. Simply select the category "Recent News" as a menu element and voilá, now you'll have a page displaying only posts from that category
I do not recommand messing with index.php
I think you should create a page template that will query posts from "Recent News" category.
Then in wordpress create a page and choose the page template you created.
In order to set it as the home page you will have to set it via wordpress configurations.
Wordpress page template
query posts by category
Set static homepage

How do I display one (featured) post from Wordpress on my static html website?

We have a Wordpress News Blog on our website (http://www.litepanels.com/news/) that is separate from the rest of the static html website. I just want to take the latest story (the featured post) and display it on our home page. The Blog and our website are on the same server. I found I can grab post titles like this:
<?php
require('../news/wp-blog-header.php');
?>
<?php query_posts('showposts=3'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?><br />
<?php endwhile;?>
but I do not know PHP well, how do I grab just the featured post (which has a title, image and text as you can see in the link above)
I am testing it here: http://www.litepanels.com/newwebsite/blog_test2.php
If that works for you, it means you've got all you need except for some way to display your information. For this, Wordpress provides some template tags that can be used within the while loop. the_content is the one you need. Others can be found in the Codex.

Categories